/*
 *  Disc Catalog
 *  A little application to quickly catalog and find contents of your
 *  DVD archives.
 *
 *  IconTextCell.m
 *  Text cell for the NSOutlineView, subclassed to show
 *  the item's icon near the title.
 *
 *  Created by Nicola Gigante on 20/12/08.
 *  Copyright (C) 2008  Nicola Gigante
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "IconTextCell.h"


@implementation IconTextCell

@synthesize image;

- (NSRect)imageRectForBounds:(NSRect)frame
{
	NSRect imageFrame = frame;
	imageFrame.size.width = NSHeight(frame);
	
	return imageFrame;
}

- (NSRect)titleRectForBounds:(NSRect)frame
{
	if (image == nil) return frame;
	NSRect imageFrame = [self imageRectForBounds:frame];
	
	NSRect textFrame = frame;
	textFrame.origin.x = NSMaxX(imageFrame);
	textFrame.size.width = NSWidth(frame) - NSWidth(imageFrame);
	
	return textFrame;
}

- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view
{
	if (image != nil) {
		NSRect imageFrame = [self imageRectForBounds:frame];
		
		BOOL imageFlipped = [image isFlipped];
		[image setFlipped:YES];
		
		[NSGraphicsContext saveGraphicsState];
		
		[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
		CGFloat squareSize = (NSHeight(frame) * (3.0/4.0));
		NSRect rect = NSInsetRect(imageFrame, (NSWidth(imageFrame) - squareSize)/2.0, (NSHeight(imageFrame) - squareSize)/2.0);
		
		[image drawInRect:rect fromRect:(NSRect){.origin = NSZeroPoint, .size = [image size]} operation:NSCompositeSourceOver fraction:1.0];
		
		[image setFlipped:imageFlipped];
		
		[NSGraphicsContext restoreGraphicsState];
	}

	[self drawInteriorWithFrame:[self titleRectForBounds:frame] inView:view];
}

@end

